﻿using System;
using System.Text;

namespace Sustainalytics.Utils
{
    public static class ExceptionUtility
    {
        const int exceptionMaxInnerLevel = 5;
        public static string GetInfo(Exception exception)
        {
            return exception != null ? exception.ToString() : string.Empty;
        }

        public static string GetInfo(Exception exception, bool includeInnerExceptions)
        {
            StringBuilder sb = new StringBuilder();
            Exception currentException = exception;
            string separator = "";
            int level = exceptionMaxInnerLevel;
            while (currentException != null && level > 0)
            {
                sb.Append(string.Format("{2}Exception:\"{0}\" StackTrace:\"{1}\"\n", currentException.Message, currentException.StackTrace, separator));
                if (!includeInnerExceptions)
                    break;
                separator = "\t";

                currentException = currentException.InnerException;
                --level;
            }
            return sb.ToString();
        }
    }
}
